package com.nisco.dms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Tbmb;
import com.nisco.dms.exception.ExistedException;
import com.nisco.dms.service.TbmbService;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;


@Controller
@RequestMapping(value = "/tbmb")
public class TbmbController {
	@Autowired
	private TbmbService tbmbService;
	private static final String CREATE = "tbmb/tbmbCreate";
	private static final String UPDATE = "tbmb/tbmbUpdate";
	private static final String LIST = "tbmb/tbmbList";

	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(Page page, String keywords,Map<String, Object> map) throws Exception {
		int maxSize = 0;
		if (StringUtils.isNotBlank(keywords)) {
			maxSize = tbmbService.search(keywords).size();
		}else{
			maxSize = tbmbService.getAll().size();
		}
		page.setTotalCount(maxSize);
		List<Tbmb> tbmbs = new ArrayList<Tbmb>();
		if (StringUtils.isNotBlank(keywords)) {
			tbmbs.addAll(tbmbService.search(keywords,(page.getPageNum()!=1?page.getPageNum()-1:0)*page.getNumPerPage(), page.getNumPerPage()));
		} else {
			tbmbs = tbmbService.getAllByPage((page.getPageNum()!=1?page.getPageNum()-1:0)*page.getNumPerPage(), page.getNumPerPage());
		}
		map.put("page", page);
		map.put("tbmbs", tbmbs);
		map.put("keywords", keywords);
		return LIST;
	}

	@ModelAttribute("preloadTbmb")
	public Tbmb getOne(@RequestParam(value = "id", required = false) String id) {
		if (id != null) {
			return tbmbService.get(id);
		}
		return null;
	}

	@RequiresPermissions("Tbmb:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("Tbmb:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String Create(Tbmb tbmb) {
		tbmbService.save(tbmb);
		AjaxObject ajaxObject = new AjaxObject(200,"添加成功！","moduleListNav_35","","","closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Tbmb:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		Tbmb tbmb = tbmbService.get(id);
		map.put("tbmb", tbmb);
		return UPDATE;
	}

	@RequiresPermissions("Tbmb:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(@ModelAttribute("preloadDept") Tbmb tbmb) {
		tbmbService.update(tbmb);

		AjaxObject ajaxObject = new AjaxObject(200,"修改成功！","moduleListNav_35","","","closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Tbmb:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject("删除成功！");
		try {
			tbmbService.delete(id);
		} catch (ExistedException e) {
			ajaxObject.setMessage(e.getMessage());
		}

		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}
}
